<?php
/**
 * 从user表同步查询数据
 * @author ray
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class("Bll_Hr_Property");
apf_require_class("Const_User");
apf_require_class("Const_Work");
apf_require_class("Const_BaseInfo");
class Oa_Job_Portal_HomeSearchUsers extends Oa_Job_Abstract {
    private $read_db = "hroa_master";//读数据库
    private $write_db = "portal_master";//写数据库
    private $bll_property;
    public function run() {
        $this->log("开始更新查询用户信息".date('Y-m-d H:i:s'));
        $this->bll_property = new Bll_Hr_Property();
        ## 1 获取所有在职人员
        $users = $this->_get_user_on_work();
        ## 2 build 数据
        $users = $this->_build_user_info($users);
        ## 3 inset or update
        $this->_inset_or_update_or_delete($users);
        $this->log("查询用户信息更新处理完...".date('Y-m-d H:i:s'));
    }
    /**
     * 在职人员
     */
    private function _get_user_on_work(){
        $sql = "SELECT user_id,work_city_id,p_level,m_level,real_name,english_name,cellphone,office_phone,office_phone_ex,user_code,office_mail FROM user WHERE job_status=".Const_User::USER_WORK_YES;
        return $this->get_db_rows($sql,$this->read_db,array());
    }
    /**
     * 构建用户信息
     * @param unknown_type $users
     * @return string
     */
    private function _build_user_info($users){

        $users = $this->_build_data_index_key($users,'user_id');

        foreach ($users as $index => $user){
            $users[$index]['office_phone'] = $user['office_phone'];
            if($user['office_phone'] && $user['office_phone_ex']){
                $users[$index]['office_phone'] .= '-';
            }
            $users[$index]['office_phone'] .= $user['office_phone_ex'];
        }
        //job
        $works = $this->_get_user_work_info();
        $works = $this->_build_data_index_key($works,'user_id');
        //position
        $jobs = $this->_get_all_job_info();
        $jobs = $this->_build_data_index_key($jobs,'job_id');
        //department
        $departments = $this->_get_all_department_info();
        $departments = $this->_build_data_index_key($departments,'department_id');
        //citys
        $citys = $this->bll_property->get_city_list();
        //bus
        $business = $this->bll_property->get_business_list();

        foreach ($users as $index => $user){
            $work = $works[$user['user_id']];
            $job  = $jobs[$work['job_id']];
            $depart = $departments[$job['department_id']];
            $city = $citys[$depart['city_id']];
            $bu   = $business[$depart['bu_id']];
            $users[$index]['bu_id'] = $depart['bu_id'];
            $users[$index]['department_id'] = $depart['function_id'];

            if($depart['function_id'] !=6 ){//技术部过滤
                $users[$index]['position_name'] = $depart['department_name'];
            }else{
                $users[$index]['position_name'] = "";
            }
            //$users[$index]['position_name'] = $city['city_name'].$bu['bu_name'].$depart['department_name'].$job['position_name'];
        }
        unset($works);
        unset($jobs);
        unset($departments);
        unset($citys);
        return $users;
    }
    /**
     * 处理结果数据
     * @param unknown_type $users
     */
    private function _inset_or_update_or_delete($users){
        $old_users = $this->_get_all_search_info();
        $old_users = $this->_build_data_index_key($old_users, 'user_id');
        foreach($users as $user){
            if(!isset($old_users[$user['user_id']])){//insert
                $this->_insert_search_user($user);
            }elseif(!$this->_compare_user($user,$old_users[$user['user_id']])){//update
                $this->_update_search_user($user);
            }
        }
        foreach($old_users as $user){
            if(!isset($users[$user['user_id']])){//insert
                $this->_delete_search_user($user);
            }
        }
        unset($old_users);
        unset($users);
    }
    /**
     *
     * 插入用户信息
     * @param unknown_type $user
     */
    private function _insert_search_user($user){
        $sql = "INSERT INTO user_search(user_id,work_city_id,bu_id,department_id,p_level,m_level,user_name,english_name,office_mail,cellphone,office_phone,user_code,position_name) VALUES(";
        $sql .= $user['user_id'];
        $sql .= ",".$user['work_city_id'];
        $sql .= ",".intval($user['bu_id']);
        $sql .= ",".intval($user['department_id']);
        $sql .= ",".$user['p_level'];
        $sql .= ",".$user['m_level'];
        $sql .= ",'".$user['real_name']."'";
        $sql .= ",'".$user['english_name']."'";
        $sql .= ",'".$user['office_mail']."'";
        $sql .= ",'".$user['cellphone']."'";
        $sql .= ",'".$user['office_phone']."'";
        $sql .= ",'".$user['user_code']."'";
        $sql .= ",'".$user['position_name']."'";
        $sql .= ")";
        $this->execute($sql,array(),$this->write_db);
    }
    /**
     * 更新用户信息
     * @param unknown_type $user
     */
    private function _update_search_user($user){
        $sql = "UPDATE user_search SET ";
        $sql .= "work_city_id=".intval($user['work_city_id']).",";
        $sql .= "bu_id=".intval($user['bu_id']).",";
        $sql .= "p_level=".intval($user['p_level']).",";
        $sql .= "m_level=".intval($user['m_level']).",";
        $sql .= "department_id=".intval($user['department_id']).",";
        $sql .= "user_name='".$user['real_name']."',";
        $sql .= "english_name='".$user['english_name']."',";
        $sql .= "office_mail='".$user['office_mail']."',";
        $sql .= "cellphone='".$user['cellphone']."',";
        $sql .= "office_phone='".$user['office_phone']."',";
        $sql .= "user_code='".$user['user_code']."',";
        $sql .= "position_name='".$user['position_name']."' ";
        $sql .= " WHERE user_id=".intval($user['user_id']);
        $this->execute($sql,array(),$this->write_db);
    }
    /**
     * 删除离职用户
     * @param unknown_type $user
     */
    private function _delete_search_user($user){
        $sql = "DELETE FROM user_search WHERE user_id=".$user['user_id'];
        $this->execute($sql,array(),$this->write_db);
    }
    /**
     * 比较用户
     * @return boolean
     */
    private function _compare_user($new,$old){
        unset($new['office_phone_ex']);
        $old['user_name'] = $old['real_name'];
        unset($new['real_name']);
        $bool = true;
        foreach($new as $key => $val){
            if($old[$key] != $val && ($old[$key] || $val)){
                $bool = false;
            }
        }
        return $bool;
    }
    /**
     * 所有查询数据
     */
    private function _get_all_search_info(){
        $sql = "SELECT * FROM user_search ";
        return $this->get_db_rows($sql,$this->write_db,array());
    }
    /**
     *所有部门
     */
    private function _get_all_department_info(){
        $sql = "SELECT department_id,function_id,department_name,city_id,bu_id,company_id FROM department ";
        return $this->get_db_rows($sql,$this->read_db,array());
    }
    /**
     * 获取所有职位
     */
    private function _get_all_job_info(){
        $sql = "SELECT job_id,position_name,department_id FROM job ";
        return $this->get_db_rows($sql,$this->read_db,array());
    }
    /**
     * 获取用户任职
     */
    private function _get_user_work_info(){
        $sql = "SELECT job_id,user_id FROM work WHERE is_parttime = ".Const_Work::WORK_MAIN;
        return $this->get_db_rows($sql,$this->read_db,array());
    }
    /**
     * 主键所有
     */
    private function _build_data_index_key($data,$index){
        $result = array();
        foreach($data as $one){
            $result[$one[$index]] = $one;
        }
        unset($data);
        return $result;
    }
}